<?php
include '../admin_head.php';
//include '../common.php';

//处理搜索分页数据
	$name = $_GET['name'];
	$status = $_GET['status'];
	
	if($name != null) $search[] = "user_name like '%{$name}%'";
	if($status != null) $search[] = "status = '{$status}'";
	
	if($search != null ) $where = 'where '. implode(' and ',$search);

	$where = empty($_GET['where']) ? $where : stripslashes($_GET['where']);

//获取总记录数
    $sql = "select count(id) num from ". PRE ."order {$where}" ;
    
    $result = mysql_query($sql);
    
    if($result && mysql_affected_rows() > 0){
        $row = mysql_fetch_assoc($result);
    }

    $total = $row['num'];       //总记录数
    //每页显示数
    $num = 10;

    //总页数
    $amount = ceil($total / $num);

    //页码
    $page = empty($_GET['p'])? 1 : (int)$_GET['p'];

    if($page < 1){
        $page = 1;
    }

    if($page > $amount){
        $page = $amount;
    }

    //偏移量
    $offset = ($page - 1) * $num;  



$sql = "select * from ".PRE."order {$where} order by addtime desc limit {$offset},{$num}";
$result = mysql_query($sql);
if($result && mysql_num_rows($result) > 0){
	while($row = mysql_fetch_assoc($result)){
		$orderlist[] = $row;
	}
}

//制作分页按钮
 $start = ($page - 3) <= 1 ? 1 : $page - 3;

    $end = ($page + 3) >= $amount ? $amount : $page + 3 ;

    for($i = $start; $i <= $end; $i++){
        if($i == $page){
            $str .= '<li><a style="color:#E50065;font-weight:bold;" href="index.php?where='.$where.'&p='. $i .'" >['. $i .']</a></li>';
            continue;
        }
            $str .= '<li><a href="index.php?where='.$where.'&p='. $i .'">['. $i .']</a><li>';

    }


    $nav = <<<tag
        <li><a href="index.php?where={$where}&p=1">首页</a></li>
        $str
        <li><a href="index.php?where={$where}&p=$amount">尾页</a></li>
tag;



?>

	<div class="admin_main_nav">订单管理&gt;&gt;订单列表</div>
	<div id="user_index" class="admin_main_index">
		<div class="width">
			<div class="user_index_title">
				<div class="user_search fr">
					<form action="index.php" method="get">
						会员名称：<input type="text" name="name"/>
						订单状态：
						<select name="status">
							<option value="">所有状态</option>
							<option value="0">等待发货</option>
							<option value="1">已发货</option>
							<option value="2">完成交易</option>
						</select>
							<input type="submit" value="搜索" />
					</form>
				</div>	
			</div>
			<div>
				<table width="800" align="center">
					<tr>
						<th>订单号</th>
						<th>会员名称</th>
						<th>收件人</th>
						<th>下单时间</th>
						<th width="90">订单状态</th>
						<th width="200">操作</th>
					</tr>
					<?php if(!empty($orderlist)):?>
					<?php foreach($orderlist as $tr):?>
					<tr>
						<td><?php echo $tr['id'] ?></td>
						<td><?php echo $tr['user_name'] ?></td>
						<td><?php 
							$sql = "select name from ".PRE."order_receiver where id='{$tr['rec_id']}'";
							$recuser = mysql_fetch_assoc(mysql_query($sql));
							echo $recuser['name'];
						?></td>
						<td><?php echo date('Y-m-d',$tr['addtime']) ?></td>
						<td><?php 
							switch($tr['status']){
								case 0:
								?>
								<div class="action">
								<div class="fl"><a class="sent" href="action.php?a=sent&id=<?php echo $tr[id] ?>"><div class="icon fl sent_i"></div><div class="fl">发货</div></a></div>
								</div>
								<?php			
									break;
								case 1:
									echo '<font color="red">等待收货</font>';
									break;
								case 2:
									echo '<font color="green">交易完成</font>';
									break;
							} 
						?></td>
						<td>
							<div class="action">
								<div class="fl"><a class="view" href="detail.php?id=<?php echo $tr[id] ?>"><div class="icon fl view_i"></div><div class="fl">订单详情</div></a></div>
								<div class="fl"><a class="del" href="action.php?a=del&id=<?php echo $tr[id] ?>&sta=<?php echo $tr['status']?>"><div class="icon fl del_i"></div><div class="fl">删除</div></a></div>
							</div>
						</td>
					</tr>
					<?php endforeach;?>
					<?php else:?>
						<tr>
							<td colspan="6">没有找到任何记录</td>
						</tr>
					<?php endif;?>
				</table>
			</div>
			<!-- 分页按钮 -->
			<div class="nav">
				<div class="tips"><?php $start = $offset + 1>0? $offset+1: 0;$end = $offset + $num >= $total? $total : $offset + $num; echo "Showing $start to $end of $total entries";?></div>
				<ul class="user_nav">
					<?php echo $nav?>
				</ul>
			</div>
		</div>
	</div>

	
<?php include '../admin_footer.php'; ?>

